package command

import (
	"log"
	"strings"
)

type Commands struct {
	list []*Command
}

func (c *Commands) Add(cmd *Command) {
	cmd.registerArgs()
	c.list = append(c.list, cmd)
}

func (c *Commands) RunCommand(input string) {
	for _, cmd := range c.list {
		if strings.HasPrefix(input, cmd.Name) {
			argMap := cmd.args.parse(input)
			if argMap == nil {
				log.Println("command args not matching")
				return
			}
			cmd.Run(argMap)
		}
	}
}

type Command struct {
	Name string
	Args func(a *Args)
	Run  func(a ArgMap)

	args Args
}

func (c *Command) registerArgs() {
	c.Args(&c.args)
}
